<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单校验</title>
    <script>
        function tipUsername(){
            document.getElementById("s1").innerHTML = "<span style='color:purple;font-size: 12px'>用户名由英文字母、数字、下划线组成,长度为4-16个字符</span>"
        }
        function checkUsername(){
            let regExp = /^\w{4,16}$/;
            let username = document.getElementById("username").value;
            if(regExp.test(username)){
                document.getElementById("s1").innerHTML = "<span style='color:green;font-size: 12px'>用户名格式正确</span>"
                return true;
            } else{
                document.getElementById("s1").innerHTML = "<span style='color:red;font-size: 12px'>您输入的用户名格式有误</span>"
                return false;
            }
        }
        function checkForm(){
            if(checkUsername()){
                return true;
            }
            return false;
        }
    </script>
</head>
<body>
<!--
    onsubmit:表单校验事件,其值为return xxx
    当值为return false的时候表单无法提交,其他值均可以提交
-->
<form action="01-引入方式.html" onsubmit="return checkForm()">
    用户名:<input type="text" id="username" onfocus="tipUsername()" onblur="checkUsername()"><span id="s1"></span><br>
    密&nbsp;&nbsp;&nbsp;码:<input type="password" id="password"><br>
    手机号:<input type="text" id="phone"><br>
    邮&nbsp;&nbsp;&nbsp;箱:<input type="text" id="email"><br>
    身份证:<input type="text" id="personId"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>